package cn.lyq.card.ui.device

import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.DeviceInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.ui.order.OrderConfirmActivity
import cn.lyq.card.utils.loadUrl
import cn.lyq.card.utils.setClick
import cn.lyq.card.utils.toMoney
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_device_detail.*


class DeviceDetailActivity : BaseActivity() {

    private var mInfo: DeviceInfo? = null
    private val mModel by lazy {
        getVM(DeviceVM::class.java)
    }

    companion object {
        private const val KEY_INFO = "INFO"

        @JvmStatic
        fun start(context: Context, info: DeviceInfo) {
            val starter = Intent(context, DeviceDetailActivity::class.java)
                .putExtra(KEY_INFO, info)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.act_device_detail
    }

    override fun initData() {
        super.initData()
        mInfo = intent.getSerializableExtra(KEY_INFO)
            ?.takeIf { it is DeviceInfo }
            ?.run { this as DeviceInfo }
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_device_detail)
    }

    override fun initListener() {
        super.initListener()
        tvConfirm.setClick {
            if (mInfo == null) {
                ToastUtils.showShort(R.string.str_data_error)
                finish()
            } else {
                mInfo?.let {
                    OrderConfirmActivity.start(this, it, etNum.text.toString().toIntOrNull() ?: 1)
                }
            }
        }
        imgAdd.setOnClickListener {
            val num = (etNum.text.toString().toIntOrNull() ?: 0) + 1
            etNum.setText(num.toString())
        }
        imgAdd.setOnLongClickListener {
            val num = (etNum.text.toString().toIntOrNull() ?: 0) + 10
            etNum.setText(num.toString())
            return@setOnLongClickListener true
        }
        imgReduce.setOnClickListener {
            val num = 0.coerceAtLeast((etNum.text.toString().toIntOrNull() ?: 0) - 1)
            etNum.setText(num.toString())
        }
        imgReduce.setOnLongClickListener {
            val num = 0.coerceAtLeast((etNum.text.toString().toIntOrNull() ?: 0) - 10)
            etNum.setText(num.toString())
            return@setOnLongClickListener true
        }
        etNum.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                val num = s.toString().toIntOrNull() ?: 0
                if (num > 9999) {
                    etNum.setText("9999")
                    setAmount(9999)
                } else {
                    setAmount(num)
                }
            }

        })
    }

    private fun setAmount(num: Int) {
        val price = mInfo?.price?.toDouble() ?: 0.0
        tvAmount.text = "￥${(price * num).toMoney()}(共${num}台)"
    }

    override fun initObserver() {
        super.initObserver()
        mModel.getDetail(mInfo?.goodsId ?: 0).observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> showLoading()
                Status.SUCCESS -> {
                    mInfo = it.requireData()
                    setData()
                    dismissLoading()
                }
                Status.ERROR -> {
                    setData()
                    ToastUtils.showShort(it.message.getMsg())
                    dismissLoading()
                }
            }
        })
    }

    private fun setData() {
        mInfo?.let {
            imgDevice.loadUrl(it.img)
            tvDeviceName.text = it.title
            tvPrice.text = "机具价格：${it.price ?: ""}元/台"
            tvPolicy.text = it.policy
            tvAmount.text = "￥${it.price ?: ""}(共1台)"
        }
    }
}